package cn.jiedh.app.meiweidk.Utils.UI;

import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.widget.Button;

import java.util.Timer;
import java.util.TimerTask;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.T;

/**
 * @name：
 * @author： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/9
 * @modifyTime： 2017/2/9
 * @explain：
 */

public class MyTBView extends AppCompatTextView {

    private long length = 60 * 1000;// 倒计时长度，默认一分钟
    private String textAfter = "s";
    private String textBefore = "获取验证码";
    private final String TIME = "time";
    private final String CTIME = "ctime";
    private Timer timer;
    private TimerTask timerTask;
    private long time;
    private Context context;

    private int textColor;

    public MyTBView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        textColor=getCurrentTextColor();
    }

    /**
     * 设置计时时候显示的文本
     *
     * @param text
     * @return
     */
    public MyTBView setTextAfter(String text) {
        this.textAfter = text;
        return this;
    }

    /**
     * 设置点击之前的文本
     */
    public MyTBView setTextBefore(String text) {
        this.textBefore = text;
        this.setText(textBefore);
        this.setTextColor(getResources().getColor(R.color.color_dadada));
        return this;
    }

    /**
     * 设置到计时长度,时间 默认毫秒
     *
     * @return
     */
    public MyTBView setLenght(long length) {
        this.length = length;
        return this;
    }


    /**
     * 开始
     */
    public void start() {
//        T.showShort("正在发送...");
        this.setEnabled(false);
        MyTBView.this.setTextColor(getResources().getColor(R.color.color_dadada));
        countDownTimer.start();
    }

    /**
     * 停止
     */
    public void stop() {
        countDownTimer.onFinish();
        MyTBView.this.setText(textBefore);
    }

    private CountDownTimer countDownTimer = new CountDownTimer(length, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            String str =  millisUntilFinished / 1000 + "秒后重发";
            setText(str);
        }

        @Override
        public void onFinish() {
            MyTBView.this.setEnabled(true);
            MyTBView.this.setTextColor(textColor);
            MyTBView.this.setText("重新发送");
        }
    };


}
